package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//游记资源控制器
@RestController
@RequestMapping("travels")
public class TravelController {
    //关联service
    @Autowired
    private ITravelService travelService;

    @PostMapping
    public JSONResult save(Travel travel){
        //保存操作,需要设置作者
        travel.setAuthor(UserContext.getCurrentUser());
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }
    //拿去给编辑页面显示
    @GetMapping("{id}")
    public Travel getById(@PathVariable Long id){
        return travelService.getById(id);
    }

    //编辑操作
    @PutMapping("{id}")
    public JSONResult update(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    //获取所有已发布的文章
    @GetMapping
    public PageInfo queryForList(TravelQueryObject qo){
        //筛选出已发布的文章
        qo.setState(Travel.STATE_RELEASE);
        //设置排序方法
        qo.setOrderby("t.releaseTime desc");
        return travelService.queryForList(qo);
    }

}
